/**
 * Created by yihua on 2017/2/27.
 */
var thumbUrl = '';
$(document).ready(function () {
    $(".editregion").each(function () {
        var id = $(this).parent().find("td").eq(0).text()
        $(this).click(function () {
            initParentId();
            console.log(id)
            //填充editregion Modal
            var rid = $(this).parent().find("td").eq(0).text();
            var parent_id = $(this).parent().find("td").eq(1).text();
            var name = $(this).parent().find("td").eq(2).text();
            var title = $(this).parent().find("td").eq(3).text();
            var is_open = $(this).parent().find("td").eq(4).text();
            var start_time = $(this).parent().find("td").eq(5).text();
            var end_time = $(this).parent().find("td").eq(6).text();
            var thumb = $(this).parent().find("a")[0].href;
            thumbUrl = thumb;
            console.log(parent_id)
            $("#edit_rid").val(rid);
            //$("#edit_parent_id").val(parent_id).trigger("change");
            $("#edit_parent_id").val(parent_id).trigger("change");
            $("#edit_name").val(name);
            $("#edit_title").val(title);
            $("#edit_is_open").find("option[text=" + is_open + "]").attr("selected", true);
            $("#edit_start_time").val(start_time);
            $("#edit_end_time").val(end_time);
            $("#edit_thumb").val(thumb);

            var content = '<img src=' + thumbUrl + ' style="width:130px">';
            $("#edit_img").fileinput('refresh', {
                uploadUrl: "/common/uploadimage",
                overwriteInitial: true,
                showUpload: false,
                uploadAsync: true,
                maxFileSize: 1500,
                showClose: false,
                showCaption: false,
                browseLabel: '',
                removeLabel: '',
                browseIcon: '<i class="glyphicon glyphicon-folder-open"></i>',
                removeIcon: '<i class="glyphicon glyphicon-remove"></i>',
                removeTitle: 'Cancel or reset changes',
                elErrorContainer: '#kv-avatar-errors-1',
                msgErrorClass: 'alert alert-block alert-danger',
                defaultPreviewContent: content,
                layoutTemplates: {main2: '{preview} ' + ' {remove} {browse}'},
                allowedFileExtensions: ["jpg", "png", "gif"]
            }).on('fileuploaded', function (event, data, id, index) {
                var ret = data.response;
                if (0 != ret.errno) {
                    toastr.error('上传失败', '', {positionClass: 'toast-bottom-center'});
                } else {
                    thumbUrl = ret.data.url;
                    console.log(thumbUrl);
                }
            }).on("filebatchselected", function (event, files) {
                $("#edit_img").fileinput("upload");
            });
            $('#edit_start_time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose: true, todayBtn: true});
            $('#edit_end_time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose: true, todayBtn: true});
            $("#editregion").modal("show")
            $("#edit_confirm").unbind('click').click(function () {
                var options = {
                    url: '/home/shop/editindexregion',
                    beforeSubmit: validate,  // pre-submit callback
                    success: editshowResponse,  // post-submit callback
                    dataType: 'json',
                    type: 'get',
                    data: {
                        'thumb': thumbUrl,
                        'rid': rid
                    }
                };
                $("#edit_regionForm").ajaxSubmit(options)
            });
        });
    });
});


function editshowResponse(responseText, statusText, xhr, $form) {
    console.log(responseText);
    if (0 !== responseText.errno) {
        toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
    } else {
        toastr.success('成功', '', {positionClass: 'toast-bottom-center'});
        $('#editregion').modal("hide");
        window.location.reload();
    }
}

function initParentId() {
    $.ajax({
        url: '/home/shop/getregioninfo',
        timeout: 100000,
        type: 'get',
        dataType: 'json',
        data: {},
        success: function (rs) {
            if (rs.errno != 0) {
                toastr.error(rs.errno);
                return false;
            } else {
                var data = [{'id': 0, 'text': '无'}];
                for (var i = 0; i < rs.data.length; i++) {
                    data.push({'id': rs.data[i].rid, 'text': rs.data[i].name})
                }
                $("#edit_parent_id").select2({
                    dropdownParent: $("#editregion"),
                    data: data
                })
            }
        },
        error: function () {
            toastr.error('系统错误');
        }
    });
}
